﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Room_Management
{
    public partial class fCheckOut : Form
    {
        private OleDbConnection myconn;
        private OleDbCommand cmd;
        int temp;
        string cbSelectItem;

        public fCheckOut()
        {
            cmd = new OleDbCommand();
            //myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""E:\Fpt University\C#\LearnCsharp\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            //this.myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\Users\claM\Desktop\LearningC#\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""Database\RoomManagement.mdb"";Persist Security Info=false");
            cmd.Connection = myconn;
            this.myconn.Open();

            InitializeComponent();

            ComboBoxAddValue();
        }

        private void ComboBoxAddValue()
        {
            cmd.CommandText = "SELECT * FROM Room WHERE Status ='Bận'";
            OleDbDataReader drRoom = cmd.ExecuteReader();
            ArrayList Rooms = new ArrayList();
            Rooms.Add(new AddValue("Chọn Phòng:", 0));
            while (drRoom.Read())
            {
                Rooms.Add(new AddValue(drRoom.GetString(1), drRoom.GetInt32(0)));
            }
            drRoom.Close();

            this.cbRoom.DataSource = Rooms;
            this.cbRoom.DisplayMember = "Display";
            this.cbRoom.ValueMember = "Value";
        }

        public class AddValue
        {
            private string m_Display;
            private long m_Value;
            public AddValue(string Display, long Value)
            {
                m_Display = Display;
                m_Value = Value;
            }
            public string Display
            {
                get { return m_Display; }
            }
            public long Value
            {
                get { return m_Value; }
            }
        }

        private void cbRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRoom.Text == "Chọn Phòng:")
            {
                bShow.Enabled = false;
                bCalculator.Enabled = false;
                MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
            }
            else
            {
                bShow.Enabled = true;
                bCalculator.Enabled = true;
            }
        }

        private void bShow_Click(object sender, EventArgs e)
        {
            cbSelectItem = cbRoom.Text;
            
            //MessageBox.Show("Item into Click: " + cbSelectItem);
            if (cbSelectItem == "Chọn Phòng:")
            {
                bShow.Enabled = false;
                bCalculator.Enabled = false;
                MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
            }
            else
            {
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "SELECT ID FROM Room WHERE RoomName ='" + cbSelectItem + "'";
                string strRoomid = cmd.ExecuteScalar().ToString();

                //MessageBox.Show("strRoomid: " + strRoomid);

                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "SELECT Customer.Name, Customer.Address, Customer.[Phone Number], Customer.CMND " +
                                  "From ((Contract INNER JOIN Customer ON (Customer.ID = Contract.CustomerID)) " +
                                                  "INNER JOIN Room ON ( Room.ID = Contract.RoomID)) " +
                                  "Where Room.ID =" + Convert.ToInt32(strRoomid);

                OleDbDataReader drResult = cmd.ExecuteReader();
                bool check = true;

                while (drResult.Read())
                {
                    tbName.Text = drResult.GetString(0);
                    tbAddress.Text = drResult.GetString(1);
                    tbPhoneNo.Text = drResult.GetString(2);
                    tbCMNDNo.Text = drResult.GetString(3);
                    check = false;
                }
                if (check == true)
                {
                    tbName.Text = " ";
                    tbAddress.Text = " ";
                    tbPhoneNo.Text = " ";
                    tbCMNDNo.Text = " ";
                    //MessageBox.Show(cbSelectItem + " chưa nhập tiền phòng hoặc thông tin cá nhân của khách hàng bị thiếu!");
                }
                drResult.Close();
            }
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            fMInterface fMain = new fMInterface();
            fMain.Show();
        }

        private void bCalculator_Click(object sender, EventArgs e)
        {
            cmd = new OleDbCommand();
            cmd.Connection = myconn;
            cmd.CommandText = " UPDATE Room SET Status = 'Trống' WHERE Status = 'Bận' AND RoomName = '" + cbSelectItem + "'";
            temp = cmd.ExecuteNonQuery();
            if (temp > 0)
            {
                MessageBox.Show("Đăng xuất thành công khách trọ " + cbSelectItem.ToLower());
            }
            else 
            {
                MessageBox.Show("Đăng xuất thất bại khách trọ " + cbSelectItem.ToLower());
            }
        }
    }
}
